//
//  CCNetWorkingTool.m
//  liveDemo
//
//  Created by 郭冰燚 on 2016/12/12.
//  Copyright © 2016年 郭冰燚. All rights reserved.
//

#import "CCNetWorkingTool.h"
#import "AFNetworking.h"
//#import "Interface.h"

#define IM_INTERFACE @"http://58.132.171.39:8088/service"

static NSString * kBaseUrl = IM_INTERFACE;

//static NSString * kBaseUrl = @"http://58.132.171.39:8088/service";
@interface CCHttpClient : AFHTTPSessionManager



@end

@implementation CCHttpClient

+ (instancetype)sharedClient{
    
    static CCHttpClient * client = nil;
    static dispatch_once_t onceToken;
    dispatch_once(&onceToken, ^{
        
        NSURLSessionConfiguration *configuration = [NSURLSessionConfiguration defaultSessionConfiguration];
        
        client.responseSerializer = [AFHTTPResponseSerializer serializer];
        client = [[CCHttpClient alloc]initWithBaseURL:[NSURL URLWithString:kBaseUrl]sessionConfiguration:configuration];
        
        client.responseSerializer = [AFHTTPResponseSerializer serializer];
        
        client.responseSerializer.acceptableContentTypes = [NSSet setWithObjects:@"application/json",@"text/html",@"text/json",@"text/javascript",@"text/plain",@"image/gif", nil];
        
        client.securityPolicy = [AFSecurityPolicy defaultPolicy];
        
        client.requestSerializer.timeoutInterval = 60;
        
    });
    
    return client;
}
@end



@implementation CCNetWorkingTool

+ (void)getWithPath:(NSString *)path params:(NSDictionary *)params success:(CCHttpSuccessBlock)success failure:(CCHttpFailureBlock)failure{
    
    NSString * url = [kBaseUrl stringByAppendingString:path];
    
    [[CCHttpClient sharedClient] GET:url parameters:params success:^(NSURLSessionDataTask * _Nonnull task, id  _Nonnull responseObject) {
        
        success(responseObject,(NSString*)task.currentRequest.URL);
    } failure:^(NSURLSessionDataTask * _Nullable task, NSError * _Nonnull error) {
       failure(error,(NSString*)task.currentRequest.URL);
    }];
    
    
//    [[CCHttpClient sharedClient]GET:url parameters:params progress:^(NSProgress * _Nonnull downloadProgress) {
//        
//    } success:^(NSURLSessionDataTask * _Nonnull task, id  _Nullable responseObject) {
//        success(responseObject);
//        
//    } failure:^(NSURLSessionDataTask * _Nullable task, NSError * _Nonnull error) {
//        failure(error);
//    }];
    
}

+ (void)postWithPath:(NSString *)path params:(NSDictionary *)params success:(CCHttpSuccessBlock)success failure:(CCHttpFailureBlock)failure{
    
    NSString * url = [kBaseUrl stringByAppendingString:path];
    
    [[CCHttpClient sharedClient]POST:url parameters:params success:^(NSURLSessionDataTask * _Nonnull task, id  _Nonnull responseObject) {
        success(responseObject,nil);
    } failure:^(NSURLSessionDataTask * _Nullable task, NSError * _Nonnull error) {
        failure(error,nil);
    }];
    
    
//    [[CCHttpClient sharedClient]POST:url parameters:params progress:^(NSProgress * _Nonnull uploadProgress) {
//        
//    } success:^(NSURLSessionDataTask * _Nonnull task, id  _Nullable responseObject) {
//        
//        success(responseObject);
//        
//    } failure:^(NSURLSessionDataTask * _Nullable task, NSError * _Nonnull error) {
//        failure(error);
//    }];
    
}


+ (void)downloadWithPath:(NSString *)path succes:(CCHttpSuccessBlock)success failure:(CCHttpFailureBlock)failure progress:(CCHttpDownloadProGressBlock)progress{
    
    
    NSString * urlString = [kBaseUrl stringByAppendingString:path];
    NSURL * url = [NSURL URLWithString:urlString];
    
    NSURLRequest * request = [NSURLRequest requestWithURL:url];
    
    
//    NSURLSessionDownloadTask * downloadTask = [[CCHttpClient sharedClient]downloadTaskWithRequest:request progress:^(NSProgress * _Nonnull downloadProgress)
//    {
//        progress(downloadProgress.fractionCompleted);
//        
//    } destination:^NSURL * _Nonnull(NSURL * _Nonnull targetPath, NSURLResponse * _Nonnull response)
//                                               
//    {
//        
//        NSURL * documentDirrctoryURL = [[NSFileManager defaultManager]URLForDirectory:NSCachesDirectory inDomain:NSUserDomainMask appropriateForURL:nil create:NO error:nil];
//        
//        return [documentDirrctoryURL URLByAppendingPathComponent:[response suggestedFilename]];
//        
//        
//    } completionHandler:^(NSURLResponse * _Nonnull response, NSURL * _Nullable filePath, NSError * _Nullable error)
//    {
//        if (error) {
//            
//            failure(error);
//            
//        }else{
//            
//            success(filePath.path);
//            
//        }
//    }];
//    
//    [downloadTask resume];
    
   }

+ (void)uploadImageWithPath:(NSString *)path parame:(NSDictionary *)parame thumbName:(NSString *)imagekey iamge:(UIImage *)image success:(CCHttpSuccessBlock)success failure:(CCHttpFailureBlock)failure progress:(CCHttpUploadProgressBlock)progress{
    
    NSString * urlString = [kBaseUrl stringByAppendingString:path];
    
    NSData * data = UIImagePNGRepresentation(image);
    
//    [[CCHttpClient sharedClient]POST:urlString parameters:parame constructingBodyWithBlock:^(id<AFMultipartFormData>  _Nonnull formData)
//    {
//        
//        [formData appendPartWithFileData:data name:imagekey fileName:imagekey mimeType:@"image/png"];
//        
//    } progress:^(NSProgress * _Nonnull uploadProgress){
//        
//        progress(uploadProgress.fractionCompleted);
//        
//    } success:^(NSURLSessionDataTask * _Nonnull task, id  _Nullable responseObject){
//        
//        success(responseObject);
//        
//    } failure:^(NSURLSessionDataTask * _Nullable task, NSError * _Nonnull error){
//        failure(error);
//    }];
    
}

@end

